//**************************************************************** 
//* Copyright (c) 2004 Ford Motor Company. All Rights Reserved. 
//* 
//* $$Workfile:   EconomicIndexDefForm.java  $$ 
//* $$Revision:   1.1  $$ 
//* $$Author:   JLOPE262  $$ 
//* $$Date:   Nov 21 2008 14:27:06  $$ 
//* 
//*****************************************************************
package ar.com.ford.it.fullEconomics.view.form;

import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import org.apache.struts.validator.ValidatorForm;
import ar.com.ford.it.fullEconomics.persistence.dto.Teese01Economic;
import ar.com.ford.it.fullEconomics.persistence.dto.Teese02Econsource;

/**
 * Form IndexDef
 * 
 * @author vcanal
 * 
 */

public class EconomicIndexDefForm extends ValidatorForm {
    
    private String create = "";
    
    private String state = "";

    private String opSelect = "opSelect";

    private String sourcFind = null;

    private String codeFind = null;

    private String descFind = null;

    private String currency = null;

    private String code = null;

    private String description = null;

    private String sourcSelec = null;

    private Collection economicSources;

    private Collection economicMeasure;

    private Collection economicIndex;

    private Teese01Economic economicIndexDefSelected;
    
    private String screen = null;

    public String getScreen() {
        return screen;
    }

    public void setScreen(String screen) {
        this.screen = screen;
    }

    public void resetForm() {

        this.state = "";
        this.codeFind = null;
        this.descFind = null;
        this.sourcFind = null;
        this.sourcSelec = null;
        this.economicSources = null;
        this.economicMeasure = null;
        this.economicIndex = null;
        this.economicIndexDefSelected = new Teese01Economic();
        economicIndexDefSelected.setEese01EconNameN("");
        economicIndexDefSelected
                .setEese01EconsourceNumberR(new Teese02Econsource());
        economicIndexDefSelected.getEese01EconsourceNumberR()
                .setEese02EconsourceNameN("");
       
        economicIndexDefSelected.setEese01EconQuantityTypeC("");
    }
    
    /**
     * Setea el EconomicIndex seleccionado para luego ser operado.
     * 
     * 
     * @param key
     * @param value
     */
    public void setEconomicIndexMapped(String key, String value) {
        List items = (List) this.getEconominIndex();
        if (items!=null){
        this.setEconomicIndexDefSelected((Teese01Economic) items
                        .get(Integer.parseInt(key) - 1));
        this.setCodeFind(this.getEconomicIndexDefSelected().getEese01EconNumberK().toString().trim());
        this.setSourcSelec(this.getEconomicIndexDefSelected().getEese01EconsourceNumberR().getEese02EconsourceNumberK().toString().trim());
        }
    }

    public String getCodeFind() {
        return codeFind;
    }

    public void setCodeFind(String codeFind) {
        this.codeFind = codeFind;
    }

    public Teese01Economic getEconomicIndexDefSelected() {
        return economicIndexDefSelected;
    }

    public void setEconomicIndexDefSelected(
            Teese01Economic economicIndexDefSelected) {
        this.economicIndexDefSelected = economicIndexDefSelected;
    }

    public Collection getEconomicMeasure() {
        return economicMeasure;
    }

    public void setEconomicMeasure(Collection economicMeasure) {
        this.economicMeasure = economicMeasure;
    }

    public Collection getEconomicSources() {
        return economicSources;
    }

    public void setEconomicSources(Collection economicSources) {
        this.economicSources = economicSources;
    }

    public Collection getEconominIndex() {
        return economicIndex;
    }

    public void setEconominIndex(Collection econominIndex) {
        this.economicIndex = econominIndex;
    }

    public String getDescFind() {
        return descFind;
    }

    public void setDescFind(String descFind) {
        this.descFind = descFind;
    }

    public String getSourcFind() {
        return sourcFind;
    }

    public void setSourcFind(String sourcFind) {
        this.sourcFind = sourcFind;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getSourcSelec() {
        return sourcSelec;
    }

    public void setSourcSelec(String sourcSelec) {
        this.sourcSelec = sourcSelec;
    }

    public String getOpSelect() {
        return opSelect;
    }

    public void setOpSelect(String opSelect) {
        this.opSelect = opSelect;
    }

    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors ae = new ActionErrors();        
        MessageResources resources = (MessageResources) request.getAttribute(org.apache.struts.Globals.MESSAGES_KEY);
        
        
        if("find".equals(this.state)){
            if ((StringUtils.isEmpty(this.getCodeFind())) &&(StringUtils.isEmpty(this.getDescFind()))){
            }else{
              
                            if (!(this.getCodeFind().matches("[0-9]+")) && (StringUtils.isNotBlank(this.getCodeFind()))) {
                                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.integergtzero", resources.getMessage("economicSource.label.code")));
                            }
                            if (this.getDescFind().matches("[0-9]+")  && (StringUtils.isNotBlank(this.getDescFind()))){
                                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("economicIndex.error.description"));
                            }
                        
                    
                
            }
        }
        
        
        if ("delete".equalsIgnoreCase(this.state)){
            if(this.getEconomicIndexDefSelected().getEese01EconNumberK()==null)
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("economicIndex.error.deletedSelect"));
        }
        
        if ("newSave".equalsIgnoreCase(this.state)){
            //if(StringUtils.isBlank(this.getEconomicIndexDefSelected().getEese01EconNumberK().toString())){
                //VER SI NO ES NULLO Y VERIFICAR LOS CAMPOS EN QUE SEA NECESARIO!
                
                //Val Update frequency
                if(StringUtils.isBlank(this.getEconomicIndexDefSelected().getEese01EconQuantityQ().toString()) || this.getEconomicIndexDefSelected().getEese01EconQuantityQ() == 0)
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("economicIndex.error.label.noFreq"));
                    if (!this.getEconomicIndexDefSelected().getEese01EconQuantityQ().toString().trim().matches("[0-9]+"))
                        ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.integergtzero", "Frecuencia"));
               //Val Description
                    String descriptionT = this.getEconomicIndexDefSelected().getEese01EconNameN();
                     if (StringUtils.isBlank((descriptionT)))
                         ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("economicIndex.error.label.Descripcion"));
              //Val Selec Drop List
                     Integer ecoNumberK = this.getEconomicIndexDefSelected().getEese01EconNumberK();
                     Integer ecoFreq = this.getEconomicIndexDefSelected().getEese01EconQuantityQ();
                     String sourcSelecT = this.getSourcSelec();
                     String currencyT = this.getCurrency();
                     String QuantityTypeC = this.getEconomicIndexDefSelected().getEese01EconQuantityTypeC();
                     if (sourcSelecT.equals("-1")) ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.required", resources.getMessage("economicIndexDef.label.source")));
                     if (currencyT.equals("-1")) ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.required", resources.getMessage("economicIndexDef.label.currency")));
                     if (QuantityTypeC.equals("-1")) ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.required", resources.getMessage("economicIndexDef.label.tipeC")));
        }
        return ae;
        
    }

    public String getCreate() {
        return create;
    }

    public void setCreate(String create) {
        this.create = create;
    }
                
}
